package sword_to_1;

public class LeeCode_33 {
    public static void main(String[] args) {
        System.out.println(verifyPostorder(new int[]{1, 6, 3, 2, 5}));
        System.out.println(verifyPostorder(new int[]{1, 3, 2, 6, 5}));
    }
    private static boolean verifyPostorder(int[] postorder) {
        return dfs(postorder.length - 1, postorder);
    }
    private static boolean dfs(int i, int[] p){
        if (i < 0){
            return true;
        }
        int base = p[i];
        boolean flag = false;
        for (int j = 0; j < i; j++) {
            if (!flag && p[j] > base){
                flag = true;
            }
            if (flag && p[j] < base){
                return false;
            }
        }
        return dfs(i - 1, p);
    }
}
